package com.bienvenue.main.ui.activity.personal;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bienvenue.main.R;
import com.bienvenue.main.adapter.HotPromptAdapter;
import com.bienvenue.main.base.BaseActivity;
import com.bienvenue.main.httpService.ResponseObserver;
import com.bienvenue.main.loader.ImageBannerLoader;
import com.bienvenue.main.model.HotJobPositionModel;
import com.bienvenue.main.model.HotPromptModel;
import com.bienvenue.main.model.HttpBean;
import com.bienvenue.main.serviceApi.ServiceModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import org.zackratos.ultimatebar.UltimateBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ProjectName: Bienvenue
 * @Package: com.bienvenue.main.ui.activity
 * @ClassName: HotPromptActivity
 * @Description: 热门公司
 * @Author: Mr.Chen
 * @CreateDate: 2020/9/8 9:19
 * @UpdateUser: Mr.Chen
 * @UpdateDate: 2020/9/8 9:19
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */


public class HotPromptActivity  extends BaseActivity implements OnBannerListener ,SwipeRefreshLayout.OnRefreshListener{
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.rv_hot_prompt_list)
    RecyclerView rvHotPromptList;
    @BindView(R.id.srl_refresh)
    SwipeRefreshLayout srlRefresh;
    HotPromptAdapter mAdapter;

    int page=1;
    @Override
    protected void initData() {



        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent=new Intent(HotPromptActivity.this,PromptDetailsActivity.class);
                startActivity(intent);
            }
        });



    }

    @Override
    protected void initView() {

        List<Integer> imgList = new ArrayList<>();
        imgList.add(R.mipmap.icon_banner_hot_prompt);
        imgList.add(R.mipmap.icon_banner_hot_prompt);
        imgList.add(R.mipmap.icon_banner_hot_prompt);
        List<String> titles = new ArrayList<>();
        titles.add("迎聘招聘1");
        titles.add("迎聘招聘2");
        titles.add("迎聘招聘3");

        srlRefresh.setOnRefreshListener(this);



        banner.setOnBannerListener(this);
        //设置banner样式
        banner.setBannerStyle(BannerConfig.NOT_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new ImageBannerLoader(this));
        //设置图片集合
        banner.setImages(imgList);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.Default);
        //设置标题集合（当banner样式有显示title时）
        banner.setBannerTitles(titles);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(1500);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
        rvHotPromptList.setLayoutManager(new LinearLayoutManager(this));
        mAdapter=new HotPromptAdapter();
        rvHotPromptList.setAdapter(mAdapter);

        onRefreshs(page);

    }

    @Override
    protected int getLayoutId() {
        UltimateBar ultimateBar = new UltimateBar(this);
        ultimateBar.setColorBar(ContextCompat.getColor(this, R.color.color_EFEFEF));
        return R.layout.activity_hot_prompt;
    }

    @OnClick(R.id.iv_return_btn)
    void onFinish(){
        this.finish();
    }
    @OnClick(R.id.iv_search_btn)
    void onSearchBtn(){
        showToast("此功能暂未实现");
    }

    @Override
    public void OnBannerClick(int position) {
        showToast("此功能暂未实现");
    }

    @Override
    public void onRefresh() {
        //模拟网络请求需要3000毫秒，请求完成，设置setRefreshing 为false
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                page = page + 1;
                List<HotPromptModel.DataBean.RecordsBean> list = onRefreshs(page);
                if (list.size() == 0) {
                    page=1;
                    onRefreshs(page);
                }
            }
        }, 3000);
    }

    private List<HotPromptModel.DataBean.RecordsBean> onRefreshs(int page) {
        List<HotPromptModel.DataBean.RecordsBean> data=new ArrayList<>();
        ServiceModel.getServiceModel().popularCompanies(0, 0, new ResponseObserver<HotPromptModel>() {
            @Override
            public void onSuccess(HotPromptModel response) {
                if (response.getCode()==0||response.getCode()==200){
                    showToast("查询成功");
                    if (response.getData().getRecords().size() > 0) {
                        data.clear();
                    }
                    data.addAll(response.getData().getRecords());
                    mAdapter.setNewData(response.getData().getRecords());
                }else{
                    showToast(response.getMsg());
                }
            }

            @Override
            public void onFail(String message) {
                showLog(message);
            }
        });
        return data;
    }
}
